<?php

/**
 * @see Zend_Controller_Plugin_Abstract
 */
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * @see Zend_Layout
 */
require_once 'Zend/Layout.php';

/**
 * Classe para configuracao do layout baseado na requisicao.
 *
 * @category Tavs
 * @package Tavs_Controller_Plugin_Layout
 *
 * @uses Zend_Layout
 *
 * @author Tales Santos <tales.augusto.santos>
 */
class Tavs_Controller_Plugin_Layout extends Zend_Controller_Plugin_Abstract
{
	/**
	 * Instancia de Zend_Layout
	 *
	 * @var Zend_Layout
	 */
	protected $_objLayout;

	/**
	 * Caminho do layout
	 *
	 * @var string
	 */
	protected $_layoutPath = '/modules/%moduleName%/views/layout';

	/**
	 * Nome do layout
	 *
	 * @var string
	 */
	protected $_layout = '%moduleName%';

	/**
	 * Constructor.
	 *
	 * Recupera a instancia de Zend_Layout
	 *
	 * @param Zend_Layout $layout [OPCIONAL]
	 *
	 * @return void
	 */
	public function __construct(Zend_Layout $layout = null)
	{
		if (null === $layout) {
			$layout = Zend_Layout::getMvcInstance();
		}

		$this->_objLayout = $layout;
		$this->_layoutPath = APPLICATION_PATH . $this->_layoutPath;
	}

	/**
	 * Chamado depois que o router acabar o roteamento.
	 *
	 * @see Zend_Controller_Plugin_Abstract::routeShutdown()
	 *
	 * @param Zend_Controller_Request_Abstract $request
	 *
	 * @return void
	 */
	public function routeShutdown(Zend_Controller_Request_Abstract $request)
	{
		//define o caminho do layout
		$moduleName = $request->getModuleName();
		$layoutPath = str_replace('%moduleName%', $moduleName, $this->_layoutPath);
		$this->_objLayout->setLayoutPath($layoutPath);

		//define o nome do layout
		$layout = str_replace('%moduleName%', $moduleName, $this->_layout);
		$this->_objLayout->setLayout($layout);
	}

}